package org.onion.pattern.sample.order.dao.pay;

import org.onion.pattern.sample.order.doamin.pay.PayOrder;
import org.onion.pattern.sample.order.doamin.pay.PayOrderId;
import org.onion.pattern.sample.order.doamin.pay.PayRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class PayRepositoryImpl implements PayRepository {

    @Autowired
    private PayDao payDao;

    @Override
    public void saveOrder(PayOrder payOrder) {
        PayOrderDO payOrderDO = new PayOrderDO();
        payOrderDO.setId(payOrder.getId().getId());
        payOrderDO.setTradeNo(payOrder.getTradeNo());
        payOrderDO.setStatus(payOrder.getStatus());
       payDao.save(payOrderDO);

    }

    @Override
    public Optional<PayOrder> getById(PayOrderId id) {
        Optional<PayOrderDO> byId = payDao.findById(id.getId());
        if (!byId.isPresent()) {
            return Optional.empty();
        }
        PayOrderDO payOrderDO = byId.get();
        PayOrder order = new PayOrder();
        order.setId(id);
        order.setStatus(payOrderDO.getStatus());
        order.setTradeNo(payOrderDO.getTradeNo());

        return Optional.of(order);
    }
}
